package cn.org.mrliu.code.generator.handler;

import java.util.ArrayList;
import java.util.List;

import cn.org.mrliu.code.generator.bean.Column;
import cn.org.mrliu.code.generator.bean.Table;
import cn.org.mrliu.code.generator.dao.DBOperation;

public class TableHandler {
	public static Table getTable(String tableName) {
		Table table = DBOperation.getTable(tableName);
		if (table!=null) {
			table.setColumns(DBOperation.getColumns(tableName));
		}
		List<Column> columns = table.getColumns();
		List<Column> baseColumns = new ArrayList<>();
		for (int i = 0; i < columns.size(); i++) {
			Column column = columns.get(i);
			String name = column.getName();
			String placeholderSimpleName = "#{"+name+"}";
			String placeholderName = "#{"+name+",jdbcType="+column.getDataType().getJdbcTypeName().toUpperCase()+"}";
			column.setPlaceholderSimpleName(placeholderSimpleName);
			column.setPlaceholderName(placeholderName);
			
			String key = column.getKey();
			//主键
			if ("PRI".equalsIgnoreCase(key)) {
				table.setPkName(name);
				table.setPkPlaceholderName(placeholderName);
				table.setPkPlaceholderSimpleName(placeholderSimpleName);
			}else{ //不是主键
				baseColumns.add(column);
			}
			if (column.getDataType().getJavaTypeName().equals("Date")) {
				table.setHasDateType(true);
			}
		}
		table.setBaseColumns(baseColumns);
		return table;
	}
}
